package com.koro.service.impl;

import com.koro.dto.FindGoodsDTO;
import com.koro.dto.SaveShopCartDTO;
import com.koro.feign.GoodsFeignClient;
import com.koro.feign.OrderFeignClient;
import com.koro.service.ShopCartService;
import com.koro.vo.FindGoodsVO;
import com.koro.vo.FindShopCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {


    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @Override
    public Integer save(SaveShopCartDTO dto) {
        return orderFeignClient.save1(dto).getData();
    }

    @Override
    public List<FindShopCartVO> findByUsersId(Integer id) {
        List<FindShopCartVO> vos = orderFeignClient.findByUserId(id).getData();
        if(vos.isEmpty()) {
            return null;
        }
        vos.forEach(s -> {
            FindGoodsDTO findGoodsDTO = new FindGoodsDTO();
            findGoodsDTO.setId(s.getGoodsId());
            FindGoodsVO findGoodsVO = goodsFeignClient.findById(findGoodsDTO).getData();
            s.setGoodsName(findGoodsVO.getGoodsName());
            s.setCurrentGoodsPrice(findGoodsVO.getPrice());
            s.setPriceFluctuations((findGoodsVO.getPrice().subtract(s.getGoodsPrice())).divide(new BigDecimal(100)));
        });

        return vos;
    }
}
